﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrokerBase.Lib.Core;
using BrokerBase.Lib.Metadata;
namespace BrokerBase.Lib.Product
{
    public class AuxProperty:IPK,IAuxProperty
    {
        private object[]  items;

        public AuxProperty()
        {

        }

        public AuxProperty(IProduct product)
        {
            Owner = product;
        }

        public static bool TryGetValueByType(string val, string dataType, out object result, out string msg)
        {

            
            if (dataType == "string")
            {
                result = val;
                msg = null;
                return true;
            }
            else if (dataType == "int")
            {
                int x = 0;
                if (int.TryParse(val, out x))
                {
                    result = x;
                    msg = null;
                    return true;
                }
                else
                {
                    result = x;
                    msg = "无法将:" + val + " 转换成整数,请重新输入!";
                    return false;
                }
            }
            else if (dataType == "double")
            {
                double d = 0;
                if (double.TryParse(val, out d))
                {
                    result = d;
                    msg = null;
                    return true;
                }
                else
                {
                    result = d;
                    msg = "无法将:" + val + " 转换成小数,请重新输入!";
                    return false;
                }
            }
            else if (dataType == "bool")
            {
                bool b = false;
                if (bool.TryParse(val, out b))
                {
                    result = b;
                    msg = null;
                    return true;
                }
                else
                {
                    result = b;
                    msg = "无法将:" + val + " 转换成布尔值,请重新输入!";
                    return false;
                }
            }
            else if (dataType == "DateTime")
            {
                DateTime dt = DateTime.Now;
                if (DateTime.TryParse(val, out dt))
                {
                    result = dt;
                    msg = null;
                    return true;
                }
                else
                {
                    result = dt;
                    msg = "无法将:" + val + " 转换成日期值,请重新输入!";
                    return false;
                }
            }
            else
            {
                result=null;
                msg = "没有匹配的数据类型，请确认数据数据的正确性!";
                return false;
            }
        }

        public static object GetValueByType(string val, string dataType)
        {
            if (dataType == "string")
                return val;
            else if (dataType == "int")
                return int.Parse(val);
            else if (dataType == "double")
                return double.Parse(val);
            else if (dataType == "bool")
                return bool.Parse(val);
            else if (dataType == "DateTime")
                return DateTime.Parse(val);
            else
                return null;
        }


        public virtual int Id { get; set; }

        public virtual IProduct Owner { get; set; }

        public virtual string DataType {get;set;}

        public virtual string Name{get;set;}

        public virtual string Value{get;set;}

        public virtual bool IsVariable{get;set;}

        public virtual string Description { get; set; }

        public virtual bool IsCollection
        {
            get{return Value.Contains(';');}
        }

        public virtual object[] ListItems 
        { 
            get
            {
                string[] ss = Value.Split(';').ToArray<string>();

                if (items == null)
                {
                    int count = ss.Count();

                    items = new object[count];

                    for (int i = 0; i < count; i++)
                    {
                        items[i] = GetValueByType(ss[i], DataType);
                    }
                }

                return items;
                
            }
        }

        public virtual object GetValue()
        {
            return GetValueByType(Value, DataType);
        }

    }
}
